package com.clothing.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;

import com.clothing.CommonAttributes;
import com.clothing.utils.Utils;

/**
 * @ClassName: Article
 * @Description: TODO(Entity-Article)
 * @author Charles.Tao
 * @date 2017年8月21日 下午4:13:45
 * @version 1.0
 */
@Entity
@Table(name = "article")
public class Article extends BaseEntity
{
    /**
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
     */
    private static final long serialVersionUID = -2378643750116345730L;
    
    /** 点击数缓存名称 */
    public static final String BROWSE_CACHE_NAME = "browseCacheName";
    
    /** 点击数缓存更新间隔时间 */
    public static final int BROWSE_CACHE_INTERVAL = 600000;
    
    public enum Status
    {
        /** 未审核 */
        unchecked,

        /** 已审核通过 */
        pass,

        /** 审核不通过 */
        notpass
    }

    private User creator;

    private String content;

    private ArticleType articleType;

    private Integer commentQuantity;

    private Status status;
    
    private String reason;
    
    private Double longitude;
    
    private Double latitude;
    
    private Integer browseQuantity;
    
    private String address;
    
    private Integer praiseQuantity;

    private Boolean deleted;

    private List<ArticleImage> images = new ArrayList<ArticleImage>();

    private List<Comment> comments = new ArrayList<Comment>();

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "creator", nullable = false)
    public User getCreator()
    {
        return creator;
    }

    public void setCreator(User creator)
    {
        this.creator = creator;
    }

    @NotBlank
    @Column(name = "content", nullable = false)
    public String getContent()
    {
        return content;
    }

    public void setContent(String content)
    {
        this.content = content;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "articleType", nullable = false)
    public ArticleType getArticleType()
    {
        return articleType;
    }

    public void setArticleType(ArticleType articleType)
    {
        this.articleType = articleType;
    }

    @Column(name = "comment_quantity", nullable = false)
    public Integer getCommentQuantity()
    {
        return commentQuantity;
    }

    public void setCommentQuantity(Integer commentQuantity)
    {
        this.commentQuantity = commentQuantity;
    }

    @ElementCollection
    @CollectionTable(name = "article_image")
    public List<ArticleImage> getImages()
    {
        return images;
    }

    public void setImages(List<ArticleImage> images)
    {
        this.images = images;
    }

    @PrePersist
    public void prePersist()
    {
        if (null == status)
        {
            setStatus(Status.unchecked);
        }
        
        setCommentQuantity(0);
        setPraiseQuantity(0);
        setBrowseQuantity(0);
        setDeleted(Boolean.FALSE);
    }

    @OneToMany(mappedBy = "article", fetch = FetchType.LAZY)
    @OrderBy("createDate asc")
    public List<Comment> getComments()
    {
        return this.comments;
    }

    public void setComments(List<Comment> comments)
    {
        this.comments = comments;
    }

    @Column(name = "reason")
    public String getReason()
    {
        return reason;
    }

    public void setReason(String reason)
    {
        this.reason = reason;
    }
    
    @Column(name = "status", nullable = false)
    public Status getStatus()
    {
        return status;
    }

    public void setStatus(Status status)
    {
        this.status = status;
    }

    @NotNull
    @Column(name = "longitude", nullable = false)
    public Double getLongitude()
    {
        return longitude;
    }

    public void setLongitude(Double longitude)
    {
        this.longitude = longitude;
    }
    
    @NotNull
    @Column(name = "latitude", nullable = false)
    public Double getLatitude()
    {
        return latitude;
    }

    public void setLatitude(Double latitude)
    {
        this.latitude = latitude;
    }

    @Column(name = "browse_quantity", nullable = false)
    public Integer getBrowseQuantity()
    {
        return browseQuantity;
    }

    public void setBrowseQuantity(Integer browseQuantity)
    {
        this.browseQuantity = browseQuantity;
    }

    @NotBlank
    @Column(name = "address", nullable = false)
    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    @Column(name = "praise_quantity", nullable = false)
    public Integer getPraiseQuantity()
    {
        return praiseQuantity;
    }

    public void setPraiseQuantity(Integer praiseQuantity)
    {
        this.praiseQuantity = praiseQuantity;
    }

    @Column(name = "deleted", nullable = false)
    public Boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    @Transient
    public List<ArticleImage> getFullImages()
    {
        if (Utils.isEmpty(images))
        {
            return images;
        }

        for (ArticleImage image : images)
        {
            if (!Utils.isEmpty(image.getImage()))
            {
                image.setImage(CommonAttributes.SITE_URL + image.getImage());
            }
        }

        return images;
    }
}
